﻿using Microsoft.AspNetCore.Mvc;
using System.Collections;
using BP.DA;
using BP.Difference;
using System.Data;
using static CCFlow.NetCore.DataUser.API.Controllers.DemoController;
using BP.Web;
using System;

namespace CCFlow.NetCore.DataUser.API.Controllers
{
    public class DeptBody
    {
        public string token { get; set; }
        public string rootNo { get; set; }
    }
     
    [Route("WF/[controller]/[Action]")]
    [ApiController]
    public class DemoController : ControllerBase
    {
        /// <summary>
        /// 系统登陆
        /// </summary>
        /// <param name="privateKey">密钥,默认:userNo+md5(userNo)+md5(password)</param>
        /// <param name="userNo">登陆账号</param>
        /// <param name="orgNo">组织编号</param>
        /// <returns>返回token</returns>
        [HttpGet, HttpPost]
        public object Port_Login(string privateKey, string userNo, string orgNo = null)
        {
            try
            {
                if (DataType.IsNullOrEmpty(userNo) == true || DataType.IsNullOrEmpty(privateKey) == true)
                    return Return_Info(500, "userNo,privateKey 任何一个都不能为空", "");

                Paras ps = new Paras();
                ps.SQL = "SELECT Pass FROM Port_Emp WHERE No=" + SystemConfig.AppCenterDBVarStr + "No";
                ps.Add("No", userNo);

                string pass = DBAccess.RunSQLReturnVal(ps) as string;
                if (DataType.IsNullOrEmpty(pass) == true)
                    return Return_Info(500, "登陆失败", "用户名或者密码错误.");

                string val = userNo + DBAccess.GenerMD5(userNo) + DBAccess.GenerMD5(pass);
                if (val.Equals(privateKey) == false)
                    return Return_Info(500, "登陆失败", "加密方法对应不上.");

                //执行本地登录.
                BP.WF.Dev2Interface.Port_Login(userNo, orgNo);
                string token = BP.WF.Dev2Interface.Port_GenerToken();

                Hashtable ht = new Hashtable();
                ht.Add("No", WebUser.No);
                ht.Add("Name", WebUser.Name);
                ht.Add("FK_Dept", WebUser.DeptNo);
                ht.Add("FK_DeptName", WebUser.DeptName);
                ht.Add("OrgNo", WebUser.OrgNo);
                ht.Add("OrgName", WebUser.OrgName);
                ht.Add("Token", token);
                // return ReturnMessage();
                return Return_Info(200, "登陆成功", BP.Tools.Json.ToJson(ht));
            }
            catch (Exception ex)
            {
                return Return_Info(500, "登陆失败", ex.Message);
            }
        }

        /// <summary>
        /// 返回信息格式:此数据格式可以根据自己的需要来定义.
        /// </summary>
        /// <param name="code">代码:200=成功，500=失败.</param>
        /// <param name="msg">消息:错误的信息.</param>
        /// <param name="data">数据:相关的json格式的数据.</param>
        /// <returns></returns>
        public static object Return_Info(int code, string msg, object data)
        {
            Hashtable ht = new Hashtable();
            ht.Add("code", code);
            ht.Add("message", msg);
            ht.Add("data", data);
            return ht;
        }

        #region 组织结构
        /// <summary>
        /// 实体集合：获得人员
        /// </summary>
        /// <returns></returns>
        [HttpGet, HttpPost]
        public object Port_Emps(string token, string deptNo)
        {
            Paras ps = new Paras();
            ps.SQL = "SELECT No,Name FROM Port_Emp WHERE FK_Dept=" + ps.DBStr + "DeptNo";
            ps.Add("DeptNo", deptNo);
            DataTable dt = DBAccess.RunSQLReturnTable(ps);
            string json = BP.Tools.Json.ToJson(dt);
            return Return_Info(200, "执行成功", json);
        }
        /// <summary>
        /// 树结构字典：获得部门
        /// </summary>
        /// <returns></returns>
        [HttpGet, HttpPost]
        public object Port_Depts(string token, string rootNo)
        {
            string sql = "SELECT No,Name,ParentNo FROM Port_Dept  where No='"+ rootNo + "'";
            DataTable dt = DBAccess.RunSQLReturnTable(sql);
            string json = BP.Tools.Json.ToJson(dt);
            return Return_Info(200, "执行成功", json);
        }
        [HttpPost]
        public object Port_DeptsJson([FromBody] DeptBody body, [FromHeader] string token)
        {
            //string token = body.token;
            string rootNo = body.rootNo;
            string sql = "SELECT No,Name,ParentNo FROM Port_Dept  where No='" + rootNo + "'";
            DataTable dt = DBAccess.RunSQLReturnTable(sql);
            string json = BP.Tools.Json.ToJson(dt);
            return Return_Info(200, "执行成功", json);
        }
        /// <summary>
        /// 查询实体：角色类型
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet, HttpPost]
        public object Port_StationTypes(string token)
        {
            string sql = "SELECT No,Name FROM Port_StationType";
            DataTable dt = DBAccess.RunSQLReturnTable(sql);
            string json = BP.Tools.Json.ToJson(dt);
            return Return_Info(200, "执行成功", json);
        }
        /// <summary>
        /// 查询实体：人员信息
        /// </summary>
        /// <returns></returns>
        [HttpGet, HttpPost]
        public object Port_Emp(string token,string empNo)
        {
            string sql = "SELECT No,Name,FK_Dept,Tel,Email FROM Port_Emp WHERE No='" + empNo + "' ";
            DataTable dt = DBAccess.RunSQLReturnTable(sql);
            string json = BP.Tools.Json.ToJson(dt);
            return Return_Info(200, "执行成功", json);
        }
        /// <summary>
        /// 过程：修改密码
        /// </summary>
        /// <param name="token"></param>
        /// <param name="empNo"></param>
        /// <param name="oldPass"></param>
        /// <param name="newPass"></param>
        /// <param name="newPass2"></param>
        /// <returns></returns>
        [HttpGet, HttpPost]
        public object Port_Emp_ChenagePassword(string token,string empNo, string oldPass, string newPass, string newPass2)
        {
            try
            {
                BP.Port.Emp emp = new BP.Port.Emp(empNo);
                emp.ChangePass(oldPass, newPass, newPass2);
                emp.CheckPass(newPass);
                return Return_Info(200, "执行成功", "密码修改成功.");
            }
            catch (System.Exception ex)
            {
                return Return_Info(500, "执行失败", "旧密码错误." + ex.Message);
            }
        }
        #endregion 组织结构.

    }
}
